<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Trie: array_node.cpp</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Trie
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">array_node.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>This example shows how to use Trie with vector to store trie node items.</p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 2012, Ranjith TV</span>
<span class="comment"> * All rights reserved.</span>
<span class="comment"> *</span>
<span class="comment"> * Licensed under the BSD 3-Clause (&quot;BSD New&quot; or &quot;BSD Simplified&quot;) license.</span>
<span class="comment"> * You may obtain a copy of the License at</span>
<span class="comment"> *</span>
<span class="comment"> * http://www.opensource.org/licenses/BSD-3-Clause</span>
<span class="comment"> *</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &lt;trie.h&gt;</span>
<span class="preprocessor">#include &lt;string&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;vector&gt;</span>
<span class="preprocessor">#include &lt;cctype&gt;</span>

<span class="keyword">class </span>TrieCaseInsensitiveCompare
{
<span class="keyword">public</span>:
    <span class="keywordtype">bool</span> operator()(<span class="keywordtype">char</span> v1, <span class="keywordtype">char</span> v2) {
        <span class="keywordtype">int</span> i1 = std::tolower(v1);
        <span class="keywordtype">int</span> i2 = std::tolower(v2);
        <span class="keywordflow">return</span> i1 &lt; i2;
    }
};

<span class="comment">// key to vector index converter</span>
<span class="comment">// case insensitive and includes alphabets, space and end symbol</span>
<span class="keyword">class </span>AlphaToIndex
{
<span class="keyword">public</span>:
    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> operator()(<span class="keyword">const</span> <span class="keywordtype">char</span> &amp; c)<span class="keyword"> const </span>{
        <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> index = 0;
        <span class="keywordflow">if</span> (c == <span class="charliteral">&#39; &#39;</span>) {
            index = 27;
        } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c &gt;= <span class="charliteral">&#39;A&#39;</span> &amp;&amp; c &lt;= <span class="charliteral">&#39;Z&#39;</span>) {
            index = c - <span class="charliteral">&#39;A&#39;</span> + 1;
        } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c &gt;= <span class="charliteral">&#39;a&#39;</span> &amp;&amp; c &lt;= <span class="charliteral">&#39;z&#39;</span>) {
            index = c - <span class="charliteral">&#39;a&#39;</span> + 1;
        }
        <span class="keywordflow">return</span> index;
    }
};

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv)
{

    <a name="_a0"></a><a class="code" href="classrtv_1_1_trie.html" title="Trie main class.">rtv::Trie</a> &lt; char, std::string,
    TrieCaseInsensitiveCompare,
    <a name="_a1"></a><a class="code" href="classrtv_1_1_vector_items.html" title="Container representing each node in the Trie.">rtv::VectorItems</a> &lt; char, std::string, TrieCaseInsensitiveCompare,
    28,  <span class="comment">// size of each node (alphabets + endsymbol + space)</span>
    AlphaToIndex <span class="comment">// key symbol to index converter</span>
    &gt;
    &gt; dictionary(<span class="charliteral">&#39;\0&#39;</span>);

    <span class="comment">// adding key value pair to the Trie</span>
    <span class="keywordflow">if</span> (dictionary.insert(<span class="stringliteral">&quot;karma&quot;</span>, <span class="stringliteral">&quot;Destiny or fate, following as effect from cause&quot;</span>).second) {
        std::cout &lt;&lt; <span class="stringliteral">&quot;added karma&quot;</span> &lt;&lt; std::endl;
    }

    <span class="comment">// removing key from the Trie</span>
    <span class="keywordflow">if</span> (dictionary.erase(<span class="stringliteral">&quot;karma&quot;</span>)) {
        std::cout &lt;&lt; <span class="stringliteral">&quot;removed karma&quot;</span> &lt;&lt; std::endl;
    }

    <span class="keywordflow">return</span> 0;
}
</pre></div> </div><!-- contents -->
</div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated on Thu Jun 21 2012 20:18:39 for Trie by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
